<?php

namespace app\common\model\xilumarket;

use think\Model;

class UserAddress extends Model
{
    // 表名
    protected $name = 'xilumarket_user_address';
    protected $hidden = ['user_id','lat','lng','createtime','updatetime'];

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'Area'
    ];

    public function getAreaAttr($value,$data){
        return [
            'province' => !empty($data['province_id']) ? Area::getNameById($data['province_id']) : '',
            'city' => !empty($data['city_id']) ? Area::getNameById($data['city_id']) : '',
            'region' => !empty($data['region_id']) ? Area::getNameById($data['region_id']) : '',
        ];
    }

    /**
     * 获取收货地址
     */
    public static function get_order_address($user_id,$address_id){
        if(!$address_id){
            $address = static::where('user_id',$user_id)->where('default_status','1')->find();
        }else{
            $address = static::where('user_id',$user_id)->where('id',$address_id)->find();
        }
        if(!$address){
            $address = static::where('user_id',$user_id)->order('updatetime','desc')->find();
        }
        return $address;
    }

}
